package com.changpay.zywy.ui.activity.login

import android.os.Handler
import com.changpay.zywy.BuildConfig
import com.changpay.zywy.R
import com.changpay.zywy.app.App
import com.changpay.zywy.base.BaseActivity
import com.changpay.zywy.base.IView
import com.changpay.zywy.constant.Constant
import com.changpay.zywy.ext.showToast
import com.changpay.zywy.ext.sss
import com.changpay.zywy.http.RetrofitHelper
import com.changpay.zywy.ui.activity.main.MainActivity
import com.changpay.zywy.utils.Preference
import me.jessyan.retrofiturlmanager.RetrofitUrlManager

class StartActivity : BaseActivity(), IView {
    override fun useEventBus(): Boolean {
        return false
    }
    override fun showLoading() {
    }

    override fun hideLoading() {

    }

    override fun showDefaultMsg(msg: String) {
        showToast(msg)
    }

    override fun showMsg(msg: String) {
        showToast(msg)
    }

    override fun showError(errorMsg: String) {
        showToast(errorMsg)
    }

    private var isFirst: Boolean by Preference(Constant.IS_FIRST, true)

    override fun attachLayoutRes(): Int {
        return R.layout.activity_start
    }

    override fun initData() {

    }

    override fun initView() {
    }

    override fun start() {
        RetrofitHelper.service.getBaseUrl().sss(this)
        {
//            RetrofitUrlManager.getInstance().setGlobalDomain(BuildConfig.SERVERHEAD)
//            App.set_do_main(BuildConfig.SERVERHEAD)  // 必须设置，否则webview打不开
            RetrofitUrlManager.getInstance().setGlobalDomain(it.result.toString())
            App.set_do_main(it.result.toString())
            Handler().postDelayed({
                if (isFirst) {
                    startActivity(GuideActivity::class.java)
                } else {
                    if (token.isEmpty()) {
                        startActivity(LoginActivity::class.java)
                    } else {
                        startActivity(MainActivity::class.java)
                    }
                }
                finish()
            }, 1000)
        }
    }
}